// packageI/newGroup/zhpgroupDetail/zhpgroupDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    goods: {},
    start_time: '',
    end_time: '',
    status: '',
    aid: '',
    active_status: '',
    countDownTime: 0,
    isAddGroup: false, //是否已参团
    btnTest: "更多活动",
    Participants: [], //参团人员
    activiyWinMember: [], //活动（非一轮）中奖人员
    showWechatshar: false,
    userMorePage: 1,
    awardMorePage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id;
    this.getData();
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
      timestamp: Date.parse(new Date()) / 1000,
      action:options.action
    });
  },
  goDetail(e){
    console.log(e);
    if(e.currentTarget.dataset.key == 'detail'){
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id='+this.data.aid+'&name=zhpGroup',
      });
    }
  },
  clickBtn(e) {
    console.log(this.data.status,this.data.isAddGroup);
    if (this.data.status && this.data.isAddGroup) {
      // 邀请好友
      return;
    } else if(this.data.status != 2 && !this.data.isAddGroup){
      // 去参团
      wx.navigateTo({
        url: '/packageA/detail_v2/detail_v2?id=' + this.data.aid + '&name=zhpGroup',
      });
      
    }else {
      // 拼团列表
      wx.navigateTo({
        url: '/packageB/member/group/GroupList/GroupList?types=zhpGroup',
      });
    }
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.zhp-group-lottery.frontend.group.get-group"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        group_id: that.data.id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            ['goods.title']: res.data.goods_title,
            ['goods.thumb']: res.data.goods_thumb,
            ['goods.price']: res.data.goods_price,
            ['goods.group_price']: res.data.group_price,
            start_time: Number(res.data.start_time),
            end_time: Number(res.data.end_time),
            aid: res.data.activity_id,
          });
          console.log(that.data.timestamp, that.data.end_time,that.data.start_time);
          if (that.data.timestamp < that.data.start_time) {
            //活动未开始
            that.data.countDownTime = that.getTimeDifference(that.start_time);
            this.setData({
              status:0,
            });
            if (res.data.member_group_info && res.data.member_group_info.order_id) {
              // 是否有参团过
              this.setData({
                isAddGroup:true,
              });
            }
            that.data.btnTest = that.data.isAddGroup ? "邀请好友" : '去参团';
            that.data.countDownTime = that.getTimeDifference(that.data.start_time);
          } else if (that.data.timestamp <= res.data.end_time && that.data.timestamp >= res.data.start_time) {
            //活动进行中
            this.setData({
              status:1,
            });
            if (res.data.member_group_info && res.data.member_group_info.order_id) {
              // 是否有参团过
              this.setData({
                isAddGroup:true,
              });
            }
            that.data.btnTest = that.data.isAddGroup ? "邀请好友" : '去参团';
            that.data.countDownTime = that.getTimeDifference(that.data.end_time);
          } else {
            this.setData({
              status:2,
            });
          }
          
          that.setData({
            countDownTime: that.data.countDownTime,
            isAddGroup: that.data.isAddGroup,
            btnTest: that.data.btnTest,
            active_status: that.data.status == 0 ? "距离活动开始时间" : (that.data.status == 1 ? "距离本轮结束" : "活动已结束")
          });
          this.getMembers(); //获取当前轮次参加会员
          this.getActiviyWinMember(); //当前活动中奖记录
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  finishChange(){
    this.getData();
  },
  getMembers() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.zhp-group-lottery.frontend.group.get-group-member"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        group_id: that.data.id,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let pants = [];
          for (let i = 0; i < res.data.data.length; i += 5) {
            pants.push(res.data.data.slice(i, i + 5));
          }
          this.setData({
            Participants: pants,
            ParticipantsLen: res.data.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  getActiviyWinMember() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.zhp-group-lottery.frontend.group.get-activiy-member-win"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        activity_id: that.data.aid,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          let pants = [];
          for (let i = 0; i < res.data.data.length; i += 5) {
            pants.push(res.data.data.slice(i, i + 5));
          }
          this.setData({
            activiyWinMember: pants,
            activiyWinMemberLen: res.data.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  swiperUser(e) {
    console.log(e);
    let key = e.currentTarget.dataset.key;
    let source = e.detail.source;
    // 是否是 自动轮播
    if (source == 'autoplay') {

      if (key == "user") {
        if (this.data.userMorePage >= this.data.ParticipantsLen) {
          return;
        } else {
          this.setData({
            userMorePage: this.data.userMorePage + 1
          });
          this.getMore('user');
        }
      }
    }
  },
  swiperUser_2(e){
    console.log(e);
    let key = e.currentTarget.dataset.key;
    let source = e.detail.source;
    // 是否是 自动轮播
    if (source == 'autoplay') {
      if (key == 'member') {
        if (this.data.awardMorePage >= this.data.activiyWinMemberLen) {
          return;
        } else {
          this.setData({
            awardMorePage: this.data.awardMorePage + 1
          });
          this.getMore('member');
        }
      }
    }
  },
  getMore(key) {
    let that = this;
    let urlStr;
    let json;
    if (key == 'user') {
      // 参团成员
      urlStr = app.getNetAddresss(
        "plugin.zhp-group-lottery.frontend.group.get-group-member"
      );
      json = {
        group_id: that.data.id,
        page: that.data.userMorePage
      };
    } else {
      // 拼中记录
      urlStr = app.getNetAddresss(
        "plugin.zhp-group-lottery.frontend.group.get-activiy-member-win"
      );
      json = {
        activity_id: that.data.aid,
        page: that.data.awardMorePage
      };
    }
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          if (key == 'user') {
            for (let i = 0; i < res.data.data.length; i += 5) {
              that.data.Participants.push(res.data.data.slice(i, i + 5));
            }
            this.setData({
              Participants: that.data.Participants,
              ParticipantsLen: res.data.last_page
            });
          } else {
            for (let i = 0; i < res.data.data.length; i += 5) {
              that.data.activiyWinMember.push(res.data.data.slice(i, i + 5));
            }
            this.setData({
              activiyWinMember: that.data.activiyWinMember,
              activiyWinMemberLen: res.data.last_page
            });
          }

        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: "none",
          });
        }
      },
    });
  },
  // 时间戳差值
  getTimeDifference(Timestamp) {
    var now = Date.parse(new Date());
    // console.log("Timestamp", Timestamp);
    let time = Timestamp < 1000000000000 ? Timestamp * 1000 : Timestamp;
    // console.log(time - now);
    if (time - now > 0) {
      return time - now;
    } else if (time - now <= 0) {
      return 0;
    }
  },
  onChange(e) {
    this.setData({
      timeData: e.detail,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userMorePage: 1,
      awardMorePage: 1
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.title ? this.data.title : "",
      path: '/packageA/detail_v2/detail_v2?id='+this.data.aid+'&name=zhpGroup',
      imageUrl:this.data.goods.thumb,
    };
  }
});